package com.motu.monstercity.server.gate;

import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.http.ServerWebSocket;

public class SocketInfo {
    // 主要
    private final static String ACCOUNT = "acc";
    private final static String USER_ID = "uid";
    private final static String SERVER_ID = "sid";
    private final static String START_TIME = "st";
    private final static String LAST_TIME = "lt";
    private final static String LANGUAGE = "lan";
    private final static String PLATFORM = "pla";

    // 附加
    private final static String CHAT_WORLD = "chatworld";
    private final static String HOSTED = "hosted";// 账号管托

    public static void setSocketInfo(ServerWebSocket socket, String account, long uid, int sid, int language,
                                     int platform) {
        socket.headers().set(ACCOUNT, account);
        socket.headers().set(USER_ID, String.valueOf(uid));
        socket.headers().set(SERVER_ID, String.valueOf(sid));
        socket.headers().set(START_TIME, String.valueOf(TimeUtils.getCurTime()));
        socket.headers().set(LANGUAGE, String.valueOf(language));
        socket.headers().set(PLATFORM, String.valueOf(platform));
    }

    /**
     * 标识是否在跨服聊天界面
     *
     * @param socket
     * @param isChatWorld 1-玩家处在跨服聊天界面,0-玩家离开了跨服聊天界面
     */
    public static void setChatWorld(ServerWebSocket socket, int isChatWorld) {
        socket.headers().set(CHAT_WORLD, String.valueOf(isChatWorld));
    }

    /**
     * 判断玩家是否处在跨服聊天界面 1-是,0-否
     */
    public static boolean isChatWorld(ServerWebSocket socket) {
        if (!socket.headers().contains(CHAT_WORLD)) {
            return false;
        }
        String value = socket.headers().get(CHAT_WORLD);
        return Integer.parseInt(value) == 1;
    }

    /**
     * 设置托管登录
     * @param isHosted 是否托管（0否 1是）
     */
    public static void setHosted(ServerWebSocket socket, int isHosted) {
        socket.headers().set(HOSTED, String.valueOf(isHosted));
    }

    /**
     * 是否托管登录
     */
    public static boolean isHosted(ServerWebSocket socket) {
        if (!socket.headers().contains(HOSTED)) {
            return false;
        }
        String value = socket.headers().get(HOSTED);
        return Integer.parseInt(value) == 1;
    }

    public static int getServerId(ServerWebSocket socket) {
        String value = socket.headers().get(SERVER_ID);
        return Tool.isEmpty(value) ? 0 : Integer.parseInt(value);
    }

    public static long getUserId(ServerWebSocket socket) {
        String value = socket.headers().get(USER_ID);
        return Tool.isEmpty(value) ? 0 : Long.parseLong(value);
    }

    public static String getAccount(ServerWebSocket socket) {
        String value = socket.headers().get(ACCOUNT);
        return Tool.isEmpty(value) ? "" : value;
    }

    public static long getStartTime(ServerWebSocket socket) {
        String value = socket.headers().get(START_TIME);
        return Tool.isEmpty(value) ? 0 : Long.parseLong(value);
    }

    public static long getLastTime(ServerWebSocket socket) {
        String value = socket.headers().get(LAST_TIME);
        return Tool.isEmpty(value) ? 0 : Long.parseLong(value);
    }

    public static void setLastTime(ServerWebSocket socket, long lastTime) {
        socket.headers().set(LAST_TIME, String.valueOf(lastTime));
    }

    public static int getLanguage(ServerWebSocket socket) {
        String value = socket.headers().get(LANGUAGE);
        return Tool.isEmpty(value) ? 0 : Integer.parseInt(value);
    }

    public static int getPlatform(ServerWebSocket socket) {
        String value = socket.headers().get(PLATFORM);
        return Tool.isEmpty(value) ? 0 : Integer.parseInt(value);
    }

}
